/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-7-20
 * 
**/
package com.julewa.db;

import org.apache.commons.beanutils.ConvertUtils;

import com.julewa.db.checkers.DataStateChecker;
import com.julewa.db.checkers._BooleanChecker;
import com.julewa.db.checkers._ByteChecker;
import com.julewa.db.checkers._CharChecker;
import com.julewa.db.checkers._DoubleChecker;
import com.julewa.db.checkers._FloatChecker;
import com.julewa.db.checkers._IntegerChecker;
import com.julewa.db.checkers._LongChecker;
import com.julewa.db.checkers._ObjectChecker;
import com.julewa.db.checkers._StringChecker;

public enum DataType {
	BOOLEAN(1, boolean.class, Boolean.class,  _BooleanChecker.class),
	CHAR   (2, char.class,    Character.class,_CharChecker.class),
	BYTE   (3, byte.class,    Byte.class,     _ByteChecker.class),
	INT    (4, int.class,     Integer.class,  _IntegerChecker.class),
	LONG   (5, long.class,    Long.class,     _LongChecker.class),
	FLOAT  (6, float.class,   Float.class,    _FloatChecker.class),
	DOUBLE (7, double.class,  Double.class,   _DoubleChecker.class),
	STRING (8, String.class,  String.class,   _StringChecker.class),
	OBJECT (0, Object.class,  Object.class,   _ObjectChecker.class);
	public static final int MIN = 1;
	public static final int MAX = 8;
	private int value;
	private String brief;
	private Class<?> primaryClass;
	private Class<?> objectClass;
	private Class<?> checkerClass;
	private DataType(int idx, Class<?> pc, Class<?> oc, Class<?> cc){
		value = idx;
		primaryClass = pc;
		objectClass = oc;
		checkerClass = cc;
		brief = name().toLowerCase();
	};
	public int getValue(){
		return value;
	}
	public String getLabel(){
		return brief;
	}
	public Class<?> getPrimaryClass(){
		return primaryClass;
	}
	public Class<?> getObjectClass(){
		return objectClass;
	}
	public boolean isPrimaryClass(Class<?> clazz){
		return !objectClass.equals(clazz);
	}
	public boolean isSystemType(){
		return value > 0;
	}
	public boolean isTextData(){
		return value == 8;
	}
	public Object convert(String str){
		if(str == null)return null;
		return ConvertUtils.convert(str, objectClass);
	}
	protected DataState _checkNumState(Object obj){
		return DataState.NORMAL;
	}
	public DataStateChecker generateChecker(){
		try {
			return (DataStateChecker) checkerClass.newInstance();
		} catch (Exception e) {
			return null;
		}
	}
	
	/////////////////////
	public static final DataType of(int value){
		for(DataType t:DataType.values()){
			if(value == t.value)return t;
		}
		return null;
	}
	public static final DataType of(String name){
		name = name.substring(0, 2).toUpperCase();
		for(DataType t:DataType.values()){
			if(t.name().startsWith(name))return t;
		}
		return null;
	}
	public static final DataType of(Class<?> clazz){
		for(DataType t:DataType.values()){
			if(t.primaryClass.equals(clazz))return t;
			if(t.objectClass.equals(clazz))return t;
		}
		return null;
	}
}
